<?php
/**
 * php GEM Project
 * 
 * LICENSE
 * 
 * PHP System Architecture Enabling Rapid Application Deployment and Scalability
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 * 
 * @copyright Copyright (C) 2010 Zeletron LLC (http://zeletron.com)
 * @license http://gem.zeletron.com/license
 */

/*
 * GEM/Form/UserUpdateForm.php
 
 * @author David Joly
 * @link http://zeletron.com
 * 
 * @category GEM
 * @package GEM_Form
 */
class GEM_Form_UserUpdateForm extends GEM_Form_Form
{
	function init(){
		$this->_formName = $form = 'userUpdateForm';
		$this->setAttrib('id',$form);
		$id = GEM_Form_UserFormElements::id($form);
		$submit = GEM_Form_UserFormElements::submit('Save');
		$email = GEM_Form_UserFormElements::email($form);
		$confirmEmail = GEM_Form_UserFormElements::confirmEmail($form,false);
		$fullname = GEM_Form_UserFormElements::fullname($form);
		$username = GEM_Form_UserFormElements::username($form);
		$info = GEM_Form_UserFormElements::info($form);
		$password = GEM_Form_UserFormElements::password($form,true);
		$password->setRequired(false);
		$confirmPassword = GEM_Form_UserFormElements::confirmPassword($form,false);
		$timezone = GEM_Form_UserFormElements::timezone($form);
		$locale = GEM_Form_UserFormElements::locale($form);

		$avatar = new Zend_Form_Element_File("{$form}_newAvatar");
		$avatar->setLabel('Upload New Avatar Image')
			 ->addValidator('MimeType',false,array(
			 		'image/jpg','image/jpeg','image/gif','image/png',
    				'messages' => array(
    	 		   		'fileMimeTypeFalse' 		=> "The file %value% has an invalid mimetype of '%type%'.",
    				  	'fileMimeTypeNotDetected' 	=> "The file's mimetype could not be detected.",
    					'fileMimeTypeNotReadable' 	=> "The file %value%, could not be read."
    				)
    		))
    		->addValidator('Extension',false,array(
    				 'png','jpg','jpeg','gif',
    				 'messages' => array(
    	 		   		'fileExtensionFalse' 	=> "The file %value% has an invalid extension.",
    				  	'fileExtensionNotFound'	=> "The file %value% was not readable."
    				  )
    		))
    		->addValidator('Size',false,array(
    				'max' => Zend_Registry::get('config')->files->images->maxfilesize,
    				'messages'  => array(
    	 		   		'fileSizeTooBig' => "The file %value% is %size% in size. The max allowed is %max%."
    				)
    		))
    		->addValidator('ImageSize',false,array(
    				'minheight' => Zend_Registry::get('config')->files->images->minheight,
    				'maxheight' => Zend_Registry::get('config')->files->images->maxheight,
    				'minwidth' 	=> Zend_Registry::get('config')->files->images->minwidth,
    				'maxwidth' 	=> Zend_Registry::get('config')->files->images->maxwidth,
    				'messages'	=> array(
    					'fileImageSizeWidthTooBig' 		=> 'Image too big. Please keep under %maxwidth% pixels in width.',
    					'fileImageSizeWidthTooSmall' 	=> 'Image too small. Please keep at least %minwidth% pixels in width.',
    					'fileImageSizeHeightTooBig' 	=> 'Image too big. Please keep under %maxheight% pixels in height.',
    					'fileImageSizeHeightTooSmall' 	=> 'Image too small. Please keep at least %minheight% pixels in height.',
    					'fileImageSizeNotDetected'		=> 'Could not detect image dimensions.',
    					'fileImageSizeNotReadable' 		=> 'Could not read image.'	
    				)
    		));
		
		$this->addElements(array($username,$fullname,$email,$confirmEmail,$timezone,$avatar,$locale,$info,$password,$confirmPassword,$id))
			 ->addDisplayGroup(array($email->getName(),$confirmEmail->getName(),$password->getName(),$confirmPassword->getName()),'credentialsGroup',array('legend' => 'Credentials','id' => "{$form}_credentialsGroup"))
			 ->addDisplayGroup(array($info->getName()),'infoGroup',array('legend' => 'Information','id' => "{$form}_infoGroup"))
			 ->addDisplayGroup(array($avatar->getName()),'avatarGroup',array('legend' => 'Avatar', 'id' => "{$form}_avatarGroup"))
			 ->addDisplayGroup(array($timezone->getName(),$locale->getName()),'settingsGroup',array('legend' => 'Settings','id' => "{$form}_settingsGroup"))
			 ->setDisplayGroupDecorators(array('FormElements','Fieldset'));
		
		//Social Display Group
		$socialTypes = Zend_Registry::get('config')->accounts->socialTypes->toArray();
		$socialElements = array();
		$socialNames = array();
		foreach($socialTypes as $type){
			$social = new Zend_Form_Element_Text($type);
			$social->addPrefixPath('GEM_Validate', 'GEM/Validate/', 'validate')
				   ->setLabel(ucfirst($type))
				   ->setFilters(array('StringToLower'))
				   ->setAttribs(array('class' => array($type,'social'),'id' => "{$form}_social-{$type}"))
				   ->addValidator('Url');
			$this->addElement($social);
			$socialElements[] = $social; 
			$socialNames[] = $social->getName();
		}
		$socialSub = new Zend_Form_SubForm();
		$socialSub->setElementsBelongTo("{$form}_social")
				  ->addElements($socialElements)
				  ->setDecorators(array('FormElements','Fieldset'));
		$this->addDisplayGroup($socialNames,'socialGroup',array('legend'=> 'Social Networking','id' => "{$form}_socialGroup"))
			 ->setDisplayGroupDecorators(array('FormElements','Fieldset'))
		->addElement($submit);
			
	}
}